﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.SqlClient;

public partial class Agency_ShowAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {

        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (PageShowName.Text.Trim() == "" || txtShowName.Text.Trim() == "")
        {
            Response.Write("<script language=JavaScript>alert('两个标题都不能为空！！！');</script>");
            return;
        }
        string _ProjectId = "", _ShowSort, _strSql, _ShowUrl = "";
        if (Request.QueryString["ProjectId"] != null)
        {
            _ProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
        }
        if (_ProjectId != "")
        {
            _ShowSort = rdoShowSort.SelectedValue;
            int _rowCun = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr;
            _strSql = "select count(ID) as cun from dbo.show where ShowProject=" + _ProjectId + " and Pagetitle='" + PageShowName.Text.Trim() + "' and ShowSort='" + _ShowSort + "'";
            dr = db1.DataReader(_strSql);
            if (dr.Read())
            {
                _rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();

            if (_rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('已经存在该项目的公示信息，不能重复添加！！！');</script>");
            }
            else
            {
                if (_ShowSort.Equals("0"))//产品公示
                {
                    _ShowUrl = "PrintWholeJJRecord.aspx";

                    _strSql = "insert into Show ( ShowName, ShowProject,ShowSort, ShowUrl, ShowDate,Pagetitle,MyTatle,MYorderby) values('" + txtShowName.Text.ToString() + "','" + _ProjectId + "','" + _ShowSort + "','" + _ShowUrl + "','" + txtcontractEndDate.Text + "','" + PageShowName.Text.Trim() + "',' CPGS" + _ProjectId + "','')";
                    db1.Command(_strSql);
                    SqlParameter[] cmdParameters = new SqlParameter[] { new SqlParameter("@v_Success", SqlDbType.Bit) };
                    cmdParameters[0].Direction = ParameterDirection.Output;
                    db1.ExecuteProcedure("P_ZZXJGS", cmdParameters);

                    db1.Close();
                }
                if (_ShowSort.Equals("2"))//挂网公示
                {
                    _ShowUrl = "PrintWholeJJRecord.aspx";

                    _strSql = "insert into Show ( ShowName, ShowProject,ShowSort, ShowUrl, ShowDate,Pagetitle,MyTatle,MYorderby) values('" + txtShowName.Text.ToString() + "','" + _ProjectId + "','" + _ShowSort + "','" + _ShowUrl + "','" + txtcontractEndDate.Text + "','" + PageShowName.Text.Trim() + "',' GWGS" + _ProjectId + "','')";
                    db1.Command(_strSql);
                    SqlParameter[] cmdParameters = new SqlParameter[] { new SqlParameter("@v_Success", SqlDbType.Bit) };
                    cmdParameters[0].Direction = ParameterDirection.Output;
                    db1.ExecuteProcedure("P_ZZGWGS", cmdParameters);

                    db1.Close();
                }
                if (_ShowSort.Equals("3"))//其他公示
                {
                    string _strTableExcul = SaveExculs().Split('.')[0].ToString();
                    if (_strTableExcul == "False") //上传 Excul
                    {
                        return;
                    }
                    _ShowUrl = "PrintWholeJJRecord.aspx";
                    _strSql = "insert into Show ( ShowName, ShowProject,ShowSort, ShowUrl, ShowDate,Pagetitle,MyTatle,MYorderby) values('" + txtShowName.Text.ToString() + "','" + _ProjectId + "','" + _ShowSort + "','" + _ShowUrl + "','" + txtcontractEndDate.Text + "','" + PageShowName.Text.Trim() + "','" + _strTableExcul + "','" + TextBox2.Text.Trim() + "')";
                    db1.Command(_strSql);

                    db1.Command(" if exists (select 1  from  sysobjects  where  id = object_id('" + _strTableExcul + "')  and   type = 'U')  drop table " + _strTableExcul + " ");

                    db1.Command(" SELECT * into " + _strTableExcul + " FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;IMEX=1;DATABASE=" + System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + _strTableExcul + ".xls',[Sheet1$])");
                    //删除空行
                    db1.Command(" delete " + _strTableExcul + " where isnull([" + TextBox2.Text.Trim() + "],'')=''");
                    db1.Close();
                }
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                // HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "Show", _strSql, "../Agency/ShowAdd.aspx");


                //if (_ShowSort.Equals("1"))
                //{
                //    _strSql = "if exists (select 1  from  sysobjects  where  id = object_id('kbgs" + _ProjectId + "')  and   type = 'U')  drop table kbgs" + _ProjectId;
                //    db1.Command(_strSql);
                //    _strSql = "Select Productid,Unit,LimitPrice,beizhu,AccountId_TB, ProjectId,goodsid,Bidid,ProductCode,ProductName,Outlookc,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,Medicinemodel,goodsname, scname as CompanyName_SC,tbrname as CompanyName_TB, tbusercode as  UserCode_TB,factoryusercode as  UserCode_sc,factor,smprice,spprice,QualityLevels into kbgs" + _ProjectId + " From BidPrice Where CheckStatus=1 And ProjectId=" + _ProjectId;
                //}
                //else if (_ShowSort.Equals("2"))
                //{
                //    //_strSql = "if exists (select 1  from  sysobjects  where  id = object_id('zbgs" + _ProjectId + "')  and   type = 'U')  drop table zbgs" + _ProjectId;
                //    //db1.Command(_strSql);
                //    //_strSql = "select bidtableid,goodsid,productname,medicinemodel,outlookc,unit,factor,cengci,companyname_sc,companyname_tb,最高零售价,最高零售价出处,bidprice from zbgs8 where isbid1=1";
                //}
                //else if (_ShowSort.Equals("3"))
                //{
                //    _strSql = "if exists (select 1  from  sysobjects  where  id = object_id('jjgs" + _ProjectId + "')  and   type = 'U')  drop table jjgs" + _ProjectId;
                //    db1.Command(_strSql);
                //    _strSql = "Select BidPrice.Bidid,ProjectId,ProductCode,ProductName,Outlookc,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,Medicinemodel,goodsname,FactoryUserCode,factor,scname,Convert(Numeric(20,4),CompeteMinPrice) AS CompeteMinPrice,Convert(numeric(20,3),CompetePackPrice) AS CompetePackPrice,QualityLevels,TBUserCode,TBRNAME Into JJgs" + _ProjectId + " From bidprice,CompeteTable Where bidprice.Projectid=" + _ProjectId + " and CheckStatus='1' And ProductCode is not null and BidPrice.Bidid=CompeteTable.Bidid And Status=1";
                //}
                //db1.Command(_strSql);
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "kbgs|zbgs|jjgs" + _ShowSort, _strSql, "../Agency/ShowAdd.aspx");

                Response.Write("<script>alert('保存成功！！！');</script>");
            }
            db1.Close();
            Response.Write("<script>window.opener.location.reload();window.close();</script>");
        }

    }
    private string SaveExculs()  //上传Excul
    {
        string sql;
        int count = 0;
        string LoadFileName = "";
        Boolean Useful = false;
        string fileName = "";
        string fileExtension = "";
        ///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;
        /// '状态信息

        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是：<hr color=red>");
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字

                HttpPostedFile postedFile = files[iFile];
                //string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                fileExtension = System.IO.Path.GetExtension(fileName);
                if (fileExtension.ToUpper() == ".XLS")
                {
                    Useful = true;
                }
                else
                {
                    Useful = false;
                }
                if (fileName != "" && Useful)
                {
                    LoadFileName = fileName;
                    strMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名：" + fileName + "<br>");
                    strMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr>");
                    ///'可根据扩展名字的不同保存到不同的文件夹

                    ///注意：可能要修改你的文件夹的匿名写入权限。

                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + fileName);

                }
            }
            if (fileName != "" && Useful)
            {
                // sql = "update project_bulletin set DownUrl='" + LoadFileName + "' where projectid=" + projectid;
                return LoadFileName;
            }
            else
            {
                if (fileName == "")
                {
                    strStatus.Text = "";
                    return "False";
                }
                else
                {
                    strStatus.Text = "只能上传文件格式(.xls)!";
                    return "False";
                }

            }
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;

            return "False";
        }

    }
}
